package com.sykj.why.controller.admin.content;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.ContentT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.ContentTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/6/5
 * @desc 后台内容管理控制访问层
 */

@RestController
@RequestMapping("/admin/content")
@Slf4j
@Api(value = "/admin/content",description = "后台内容管理控制访问层")
public class ContentController {

    @Autowired
    private ContentTService contentTService;

    /**
     * 添加内容
     * @param contentT
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "/add",notes = "添加内容")
    public ResponseEntity add(@RequestBody ContentT contentT){
        try {
            contentTService.saveContent(contentT);
            return Response.success("添加成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("添加内容出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 删除内容
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/remove",notes = "删除内容")
    public ResponseEntity remove(@PathVariable String id){
        try {
            contentTService.removeContent(id);
            return Response.success("删除成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("删除内容出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改内容
     * @param contentT
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "/update",notes = "修改内容")
    public ResponseEntity update(@RequestBody ContentT contentT){
        try {
            contentTService.updateContent(contentT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改内容出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 启用/禁用内容
     * @param id
     * @param flag
     * @return
     */
    @RequestMapping(value = "/enable",method = RequestMethod.POST)
    @ApiOperation(value = "/enable",notes = "启用/禁用内容")
    public ResponseEntity enable(@RequestParam String id,@RequestParam boolean flag){
        try {
            contentTService.disableOrEnable(id, flag);
            return Response.success("操作成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("启用禁用内容出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 查询全部内容
     *
     * @return 全部内容
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value ="/findAll",notes = "查询全部内容")
    public ResponseEntity findAll() {
        try {
            return Response.success("查询成功", contentTService.findAll());
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询全部内容出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 查询专辑下的内容
     * @param albumId
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/findContentByAlbum",method = RequestMethod.POST)
    @ApiOperation(value = "/findContentByAlbum",notes = "查询专辑下的内容")
    public ResponseEntity findContentByAlbum(@RequestParam String albumId,@RequestParam String categoryId){
        try {
            return Response.success("查询成功", contentTService.findByAlbum(albumId,categoryId));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询内容出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
